<template>
    <div class="login lazyImage">
        <div class="container" :class="toogle ? 'right-panel-active' : ''">
            <!-- 登录 -->
            <div class="container__form container--signup">
                <WithWait :loading="loading">
                    <el-form ref="ruleFormRef" class="form" :model="loginForm" :rules="rules" status-icon>
                        <h2 class="form__title">Welcome To Back!</h2>
                        <el-form-item label="账号" prop="username">
                            <el-input v-model="loginForm.username" />
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input v-model="loginForm.password" show-password />
                        </el-form-item>
                        <el-form-item style="margin-top:35px" label-width="0px">
                            <button class="btn" v-throttle="{ click: () => login(ruleFormRef), time: 3000 }">登录</button>
                        </el-form-item>
                    </el-form>
                </WithWait>
            </div>

            <!-- 注册 -->
            <div class="container__form container--signin">
                暂未开通......
            </div>
            <!-- 切换按钮 -->
            <div class="container__overlay">
                <div class="overlay lazyImage">
                    <div class="overlay__panel overlay--left">
                        <button class="btn" @click="toogle = !toogle">
                            <svg-icon class="left-arrow" name="left-arrow"></svg-icon>
                            <u>注 册</u>
                        </button>
                    </div>
                    <div class="overlay__panel overlay--right">
                        <button class="btn" @click="toogle = !toogle">
                            <u>登 录</u>
                            <svg-icon class="right-arrow" name="right-arrow"></svg-icon>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script lang="ts">
export default {
    name: "Login"
}
</script>
<script lang='ts' setup>
import { reactive, ref } from 'vue';
import { vThrottle } from '@/utils/custom-directive' // 自定义指令
import { ElNotification, FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router';
import { useLogin, useSystem } from '@/pinia';
import { loginUser } from '@/api/login';
import { handleResponse } from '@/utils/responseCode';
import BackGroundImage from "@/assets/login-background.png"
const router = useRouter()
const LoginStore = useLogin()

const SystemStore = useSystem()
// 账号登录
const loginForm = reactive({
    username: "admin",
    password: "123456"
})
const rules: FormRules = {
    username: [
        { required: true, message: '请输入用户名称', trigger: 'blur' },
        { min: 1, max: 12, message: '长度在3-12之间', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '密码', trigger: 'blur' },
        { min: 1, max: 12, message: '长度在6-18之间', trigger: 'blur' },
    ],
    visitorName: [
        { required: true, message: '请输入游客名称', trigger: 'blur' },
        { min: 3, max: 12, message: '长度在3-12之间', trigger: 'blur' },
    ],
}
const ruleFormRef = ref<FormInstance>()

let loading = ref<boolean>(false)
const login = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    loading.value = true

    await formEl.validate(async (valid, fields) => {

        if (!valid) {
            loading.value = false
            ElNotification({
                title: 'Error',
                message: '账号或密码格式不正确！',
                type: 'error',
            })
            return
        }
        let res = await loginUser(loginForm)

        if (!res) return loading.value = false

        const { data, code, msg } = res.data

        if (!handleResponse(code, msg)) {
            ElNotification({
                title: '登录失败',
                message: "账号或密码不正确",
                type: 'error',
            })
            loading.value = false
            return
        }
        // 存储token
        LoginStore.$patch({
            token: data.token
        })
        SystemStore.$patch({
            permission: data.permissions,  // 存储权限
            userinfo: data.userInfo,  // 存储用户信息
            router: data.menus  // 存储路由
        })
        router.push({
            path: '/home',
            replace: true
        })
        ElNotification({
            title: '登录成功',
            message: '正在跳转至首页',
            type: 'success',
        })
    })
}
const toogle = ref(true)

let img = new Image()
img.src = BackGroundImage
img.onload = () => {
    let lazy = document.querySelectorAll(".lazyImage")
    for (let i = 0; i < lazy.length; i++) {
        const element = lazy[i] as HTMLDivElement

        element.className = element.className + " image"
    }
}

</script>

<style lang="less" scoped>
@fontsize: 2rem;

.image {
    background: url("/src/assets/login-background.png");
}

.login {
    width: 100%;
    height: 100%;
    // background: url("/src/assets/login-background.png");
    /* 决定背景图像的位置是在视口内固定，或者随着包含它的区块滚动。 */
    /* https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-attachment */
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;

}

.form__title {
    font-size: @fontsize * 1.5;
    font-weight: 300;
    margin: 0;
    margin-bottom: 60px;
    color: var(--color);
}

.container {
    border-radius: 0.7rem;
    box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25),
        0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
    // height: 420px;
    // max-width: 758px;
    width: 50%;
    height: calc(50% / 1);
    overflow: hidden;
    position: relative;
    font-family: Arial, sans-serif !important;
}

.container__form {
    height: 100%;
    position: absolute;
    top: 0;
    transition: all 0.6s ease-in-out;

}

.container--signin {
    left: 0;
    width: 50%;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
}

.container.right-panel-active .container--signin {
    transform: translateX(100%);
}

.container--signup {
    left: 0;
    opacity: 0;
    width: 50%;
    z-index: 1;
}

.container.right-panel-active .container--signup {
    animation: show 0.6s;
    opacity: 1;
    transform: translateX(100%);
    z-index: 5;
}

.container__overlay {
    height: 100%;
    left: 50%;
    overflow: hidden;
    position: absolute;
    top: 0;
    transition: transform 0.6s ease-in-out;
    width: 50%;
    z-index: 100;

    .overlay {
        background-color: #008997;
        // background: url("/src/assets/login-background.png");
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
        height: 100%;
        left: -100%;
        position: relative;
        transform: translateX(0);
        transition: transform 0.6s ease-in-out;
        width: 200%;

        .overlay__panel {
            align-items: center;
            display: flex;
            flex-direction: column;
            height: 100%;
            justify-content: center;
            position: absolute;
            text-align: center;
            top: 0;
            transform: translateX(0);
            transition: transform 0.6s ease-in-out;
            width: 50%;

            u {
                display: flex;
                height: 100%;
                align-items: center;
                text-decoration: none;
            }

            .left-arrow,
            .right-arrow {
                font-size: 2rem;
            }
        }

        .overlay--left {
            transform: translateX(-20%);
        }

        .overlay--right {
            right: 0;
            transform: translateX(0);
        }

    }
}

.container.right-panel-active .container__overlay {
    transform: translateX(-100%);
}

.container.right-panel-active .overlay {
    transform: translateX(50%);
}

.container.right-panel-active .overlay--left {
    transform: translateX(0);
}


.container.right-panel-active .overlay--right {
    transform: translateX(20%);
}

:deep(.button) {
    background-color: #0367a6;
    background-image: linear-gradient(90deg, #0367a6 0%, #008997 74%);
    border-radius: 20px;
    border: 1px solid #0367a6;
    color: #e9e9e9;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    letter-spacing: 3px;
    padding: 20px 60px;
    text-transform: uppercase;
    transition: transform 80ms ease-in;
}



.form {
    background-color: var(--background);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0px 30px;
    height: 100%;
    text-align: center;

    .el-input {
        height: 50px;
        font-size: @fontsize;
        color: var(--color);
    }

    :deep(.el-form-item__label) {
        height: 50px;
        line-height: 50px;
        font-size: @fontsize;
    }
}

:deep(.el-icon) {
    font-size: @fontsize;
}

.input {
    background-color: #fff;
    border: none;
    padding: 0.9rem 0.9rem;
    margin: 0.5rem 0;
    width: 100%;
}

@keyframes show {

    0%,
    49.99% {
        opacity: 0;
        z-index: 1;
    }

    50%,
    100% {
        opacity: 1;
        z-index: 5;
    }
}

:deep(.el-form-item__label) {
    font-family: Arial, sans-serif !important;
}

.btn {
    padding: 10px 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    cursor: pointer;
    user-select: none;
    letter-spacing: 1rem;
    text-indent: 1rem;
    border-radius: 20px;
    box-sizing: border-box;
    overflow: hidden;
    position: relative;
    border: 2px solid var(--active-color);
    color: #fff;
    ;
    transition: background-color .2s;
    background-color: var(--active-color);

    &::before {
        content: "";
        position: absolute;
        width: 50px;
        height: 200%;
        background-color: rgba(255, 255, 255, 0.4);
        transform: skew(45deg) translate3d(-200px, 0, 0);
    }

    &:hover {

        &::before {
            transition: ease-in-out .5s;
            transform: skew(45deg) translate3d(300px, 0, 0);
        }
    }

}
</style>